package com.playball.lottery.frm.yhzx;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.playball.lottery.R;
import com.playball.lottery.base.BaseActivity;
import com.playball.lottery.customview.ShowCustomDialog;
import com.playball.lottery.customview.ShowTipDialog;
import com.playball.lottery.nt.GetRequestParams;


public class JddFindPassActivity extends BaseActivity implements OnClickListener {

	private TextView tv_title, btn_by_phone;
	private TextView btn_title_left;
	private Button btn_next;
	private EditText et_userName;
	private EditText et_phone;
	private ShowTipDialog showdialog = new ShowTipDialog();
	private String userName, userPhone;
	// 传递过来的用户名
	private String intent_userName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.jdd_find_pass);
		// 获取传递过来的用户名
		intent_userName = getIntent().getStringExtra("user_name");

		tv_title = (TextView) findViewById(R.id.top_title_info);
		btn_title_left = (TextView) findViewById(R.id.top_title_back);
		et_userName = (EditText) findViewById(R.id.et_user_name);
		et_phone = (EditText) findViewById(R.id.et_phone);
		btn_next = (Button) findViewById(R.id.btn_next);
		btn_by_phone = (TextView) findViewById(R.id.btn_by_phone);
		btn_by_phone.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);// 下划线
		tv_title.setText("找回密码");
		// 判断用户名
		if (!intent_userName.equals("") && intent_userName != null) {
			et_userName.setText(intent_userName);
		}
		btn_title_left.setOnClickListener(this);
		btn_next.setOnClickListener(this);
		btn_by_phone.setOnClickListener(this);

	}

	public class AsyncLoader extends AsyncTask<String, Integer, String> {// 继承AsyncTask

		// 1.准备运行
		protected void onPreExecute() {
			showdialog.showDialog(JddFindPassActivity.this);
		}

		// 2.正在后台运行
		@Override
		protected String doInBackground(String... params) {
			String strResult;
			try {
				userName = et_userName.getText().toString();
				userPhone = et_phone.getText().toString();
				JSONObject param = new JSONObject();
				param.put("username", userName);
				param.put("mobile", userPhone);
				String action = params[0];
				// 获取请求返回值
				strResult = GetRequestParams.getRequestResult(param.toString(), action, getApplicationContext());
				return strResult;
			} catch (Exception e) {
				// TODO: handle exception
				Log.e("error", e.toString());
				return null;
			}
		}

		// 完成后台任务
		protected void onPostExecute(String result) {
			if (result != null) {
				try {

					JSONTokener jsonParser = new JSONTokener(result);
					JSONObject js = (JSONObject) jsonParser.nextValue();
					// 接下来的就是JSON对象的操作了
					if (Integer.parseInt(js.getString("Code")) == 0) {
						Intent intent = new Intent();
						intent.setClass(JddFindPassActivity.this, JddVerificationActivity.class);
						intent.putExtra("username", userName);
						intent.putExtra("mobile", userPhone);
						startActivity(intent);
						setResult(RESULT_OK);
						finish();
					}
					Toast.makeText(JddFindPassActivity.this, js.getString("Data"), Toast.LENGTH_SHORT).show();
				} catch (JSONException ex) {
					// 异常处理代码
					Log.e("error", ex.toString());
				} finally {
					showdialog.closeDialog();
				}
			} else {
				Toast.makeText(JddFindPassActivity.this, getString(R.string.problem_01), Toast.LENGTH_LONG).show();
			}
		}

	}

	public void onResume() {
		super.onResume();
		
	}

	public void onPause() {
		super.onPause();
		
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// setResult(RESULT_CANCELED);
			this.finish();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	};

	// 判断手机号
	public static boolean isMobileNO(String mobiles) {

		Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-3,5-9]))\\d{8}$");

		Matcher m = p.matcher(mobiles);
		return m.matches();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.top_title_back:
			finish();
			break;
		case R.id.btn_next:
			try {
				// close keyboard
				InputMethodManager inputmanger = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
				inputmanger.hideSoftInputFromWindow(btn_next.getWindowToken(), 0);
				if (et_userName.getText().toString().equals("")) {
					Toast.makeText(JddFindPassActivity.this, R.string.enter_your_user_name, Toast.LENGTH_SHORT).show();
				} else if (et_phone.getText().toString().equals("")) {
					Toast.makeText(JddFindPassActivity.this, R.string.input_binding_mobile_phone_number,
							Toast.LENGTH_SHORT).show();
				} else if (et_phone.getText().length() != 11 || !isMobileNO(et_phone.getText().toString())) {
					Toast.makeText(JddFindPassActivity.this, R.string.input_eleven_digit_mobile_phone_number,
							Toast.LENGTH_SHORT).show();

				} else {
					AsyncLoader task = new AsyncLoader();
					task.execute("1031");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case R.id.btn_by_phone:
			ShowCustomDialog alertDialog = new ShowCustomDialog.Builder(this).setTitle("提示")
					.setMessage("拨打18674899170？").setPositiveButton("确定", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							Intent intent = new Intent();
							intent.setAction("android.intent.action.DIAL");
							intent.setData(Uri.parse("tel:18674899170"));
							startActivity(intent);
							dialog.cancel();
						}
					}).setNegativeButton("取消", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();
						}
					}).createCustomDialog();
			alertDialog.show();
			break;
		default:
			break;
		}

	};
}
